﻿using System;
using System.Windows;
using System.Windows.Controls;
using QSBusinessLayerCSharp;

namespace QSWPFApplicationCSharp
{
    /// <summary>
    /// Interaction logic for BasicWindow.xaml
    /// </summary>
    public partial class BasicWindow : Window
    {
        private readonly Person person;
        public BasicWindow()
        {
            var mapper = new PersonMapper();
            person = mapper.GetItem(1);
            DataContext = person;
            InitializeComponent();
        }


        private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            person.PropertyValidationManager.ValidateAll();
            if (person.IsValid)
            {
                Close();
            }
            else
            {
                errorTreeView.Items.Clear();
                foreach (var result in person.ValidatorResultsInError)
                {
                    var node = new TreeViewItem {Header = result.ErrorMessage};
                    node.Items.Add(String.Format("RuleInterpretation = '{0}'", result.Rule.RuleInterpretation));
                    node.Items.Add(String.Format("Rule = '{0}'", result.Rule.GetType()));
                    errorTreeView.Items.Add(node);
                }
                MessageBox.Show(string.Format("Data is invalid. Errors:\r\n{0}", person.Error));
            }
        }
    }

}